﻿using System.IO;
using File = TagLib.File;

namespace SkyJukebox
{
    public class Music
    {
        public Music(string filePath)
        {
            Initialize(filePath);
        }
        private string _path;
        public string FilePath
        {
            get { return _path; }
            set { Initialize(value); }
        }
        public string FileName
        {
            get
            {
                return _path.SubstringRange(_path.LastIndexOf('\\') + 1, _path.LastIndexOf('.'));
            }
        }
        public Codec Codec { get; private set; }

        public File TagFile { get; private set; }

        public FileInfo GetFileInfo()
        {
            return new FileInfo(FilePath);
        }

        private void Initialize(string filePath)
        {
            _path = filePath;
            Codec = GetCodec(GetFileInfo());
            TagFile = File.Create(_path);
            //System.Windows.Forms.MessageBox.Show(TagFile.Properties.Codecs.ToArray()[0].Description + " " + TagFile.Tag.Title);
        }

        public static Codec GetCodec(FileInfo file)
        {
            if (!file.Exists)
                throw new FileNotFoundException("File not found: " + file);
            switch (file.Extension.ToLower())
            {
                case ".mp3":
                    return Codec.Mp3;
                case ".wav":
                    return Codec.Wave;
                case ".flac":
                    return Codec.Flac;
                case ".ogg":
                    return Codec.OggVorbis;
                case ".wma":
                    return Codec.Wma;
                case ".aiff":
                    return Codec.Aiff;
                case ".ape":
                    return Codec.Ape;
                case ".m4a":
                    return Codec.M4A;
                case ".mpc":
                    return Codec.Mpc;
                case ".ac3":
                    return Codec.Ac3;
                case ".aac":
                    return Codec.Aac;
                default:
                    return Codec.Unknown;
            }
        }
    }

    public enum Codec
    {
        Unknown, Wave, Mp3, OggVorbis, Flac, Wma, Aac, Ac3, Aiff, M4A, Mpc, Ape
    }
}
